/**
 * RmiId.java
 */
package fss.util.rmi;

import java.io.Serializable;;

/**
 * An interface publishing the RMI identification for an
 * object registered in RMI.
 * 
 * @author thinh
 *
 */
public class RmiId implements Serializable {
    private static final long serialVersionUID = 1L;
    /**
     * The host name.
     */
    private String host = null;
    /**
     * The object name on RMI.
     */
    private String name = null;
    /**
     * The RMI port.
     */
    private int port;
    /**
     * Constructor.
     * @param host the host.
     * @param name the object name.
     * @param port the port.
     */
    public RmiId(String host, String name, int port) {
        if(host == null || name == null) throw new IllegalArgumentException("Invalid RmiId " + host + ", " + port + ", " + name);
        this.host = host;
        this.port = port;
        this.name = name;
    }
    /**
     * Get the hostname.
     * @return the hostname.
     */
    public String getHost() { return host; }
    /**
     * Get the identifier name.
     * @return the name.
     */
    public String getName() { return name; }
    /**
     * Get the RMI port.
     * @return the RMI port.
     */
    public int getPort() { return port; }
    /**
     * Get the fully qualified RMI url.
     * <p>
     * URL format: <code>rmi://host:port/name</code>.
     * @return the url.
     */
    public String getRmiUrl() { 
        StringBuilder url = new StringBuilder();
        url.append("rmi://" + host + ":" + port + "/" + name);
        return url.toString();
    }
    @Override
    public boolean equals(Object obj) {
        if(obj == null) return false;
        if(!(obj instanceof RmiId)) return false;
        
        RmiId objId = (RmiId) obj;
        return (objId.getHost().equals(this.getHost()) && objId.getName().equals(this.getName()) && objId.getPort() == this.getPort());
    }
    @Override
    public int hashCode() {
        return (getHost() + getName() + getPort()).hashCode();
    }
    @Override
    public String toString() {
        return name + "@" + host + ":" + port;
    }
}
